package model

import (
	"time"

	"gddgame.cc/galaxy/utils"
	"github.com/speps/go-hashids"
)

const MapName = "map"

type Map struct {
	Id       int    `json:"id" orm:"auto" sql:"INT"`
	UserId   int    `json:"user_id" sql:"INT"`
	Name     string `json:"name" sql:"VARCHAR"`
	Content  string `json:"content" sql:"VARCHAR"`
	People   int    `json:"people" sql:"INT"`
	No       int    `json:"no" sql:"INT"`
	AiNumber int    `json:"ai_number" sql:"ai_number"`

	Version     int                    `json:"version" sql:"INT"`
	Data        []byte                 `json:"data" sql:"MEDIUMBLOB"`
	CostList    DataRelationSlice      `json:"cost_list" sql:"JSON"`
	MessageList DataSliceRelationSlice `json:"message_list" sql:"JSON"`
	Review      bool                   `json:"review" sql:"TINYINT"`

	CreateTime *time.Time `json:"create_time" orm:"createTime" sql:"DATETIME"`
	UpdateTime *time.Time `json:"update_time" orm:"updateTime" sql:"DATETIME"`

	hashIds *hashids.HashID
}

func (app *Map) initHash() {
	if app.hashIds == nil {
		app.hashIds, _ = hashids.NewWithData(&hashids.HashIDData{Alphabet: hashids.DefaultAlphabet, MinLength: 6, Salt: utils.ToString(app.UserId)})
	}
}

func (app *Map) EncodeNo(no int) string {
	app.initHash()
	id, _ := app.hashIds.Encode([]int{app.Id, no})
	return id
}
